﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Page_UpdataRule : UIPage
    {
        public Page_UpdataRule()
        {
            InitializeComponent();
        }
        public string name { get; set; }
        int clickIndex;
        db_mmsEntities conn = new db_mmsEntities();
        private void Page_UpdataRule_Load(object sender, EventArgs e)
        {
            uiDataGridView1.SetRowHeight(30);
            inIt();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            showView();
        }
        /// <summary>
        /// 修改规则
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            string ru = txt_RuleNm.Text;
            int rtype;
            switch (com_RuleTp.Text)
            {
                case "null":
                    rtype = -1;
                    break;
                case "白金卡":
                    rtype = 0;
                    break;
                case "金卡":
                    rtype = 1;
                    break;
                case "银卡":
                    rtype = 2;
                    break;
                default:
                    MessageBox.Show("会员类型不可修改！");
                    return;
            }
            
            var temp = conn.tb_rule.ToList().FirstOrDefault(r => r.ruleName == ru && r.memberType == rtype);
            if (temp == null)
            {
                MessageBox.Show("未查询到该规则，请添加规则！");
                return;
            }
            string adg = txt_AddGr.Text;
            string dis = txt_Dis.Text;
            temp.addGread = Convert.ToInt32(adg);
            temp.disCount = dis;
            conn.SaveChanges();
            MessageBox.Show("规则修改成功");
            showView();

        }

        private void showView()
        {
            uiDataGridView1.Rows.Clear();
            var r = conn.tb_rule.Where(s => s.storeName == name).ToList();
            if (r.Count==0)
            {
                MessageBox.Show("未查询到规则信息！");
                return;
            }
            foreach(var i in r)
            {
                string mtype = "";
                switch (i.memberType)
                {
                    case -1:
                        mtype = "null";
                        break;
                    case 0:
                        mtype = "白金卡";
                        break;
                    case 1:
                        mtype = "金卡";
                        break;
                    case 2:
                        mtype = "银卡";
                        break;
                    default:
                        MessageBox.Show("异常！");
                        return;
                }
                string[] ru = new string[] {i.storeName,i.ruleName,mtype,i.addGread.ToString(),i.disCount };
                uiDataGridView1.Rows.Add(ru);
            }
            inIt();
        }

        private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            clickIndex = e.RowIndex;
            //MessageBox.Show(index.ToString());
            //所在行商品编号
            txt_RuleNm.Text = uiDataGridView1.Rows[clickIndex].Cells[1].Value.ToString();
            com_RuleTp.Text = uiDataGridView1.Rows[clickIndex].Cells[2].Value.ToString();
            txt_AddGr.Text = uiDataGridView1.Rows[clickIndex].Cells[3].Value.ToString();
            txt_Dis.Text = uiDataGridView1.Rows[clickIndex].Cells[4].Value.ToString();

        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            string ru = txt_RuleNm.Text;
            int rtype;
            switch (com_RuleTp.Text)
            {
                case "null":
                    rtype = -1;
                    break;
                case "白金卡":
                    rtype = 0;
                    break;
                case "金卡":
                    rtype = 1;
                    break;
                case "银卡":
                    rtype = 2;
                    break;
                default:
                    MessageBox.Show("会员类型有误！");
                    return;
            }

            var temp = conn.tb_rule.ToList().FirstOrDefault(r => r.ruleName == ru && r.memberType == rtype);
            if (temp != null)
            {
                MessageBox.Show("该规则已存在，不可添加该规则！");
                return;
            }
            string adg = txt_AddGr.Text;
            string dis = txt_Dis.Text;
            temp.ruleName = ru;
            temp.memberType = rtype;
            temp.addGread = Convert.ToInt32(adg);
            temp.disCount = dis;
            conn.SaveChanges();
            MessageBox.Show("规则添加成功");
            showView();
        }
        private void inIt()
        {
            List<string> ty = new List<string>();
            var t = conn.tb_rule.GroupBy(r => r.memberType).Select(k=>k.Key).ToList();
            foreach(var i in t)
            {
                string mtype = "";
                switch (Convert.ToInt32(i))
                {
                    case -1:
                        mtype = "null";
                        break;
                    case 0:
                        mtype = "白金卡";
                        break;
                    case 1:
                        mtype = "金卡";
                        break;
                    case 2:
                        mtype = "银卡";
                        break;
                    default:
                        MessageBox.Show("异常！");
                        return;
                }
                ty.Add(mtype);
            }
            com_RuleTp.DataSource = ty;
        }
    }
}
